package com.deyuanyun.pic.pipe.controller.publiccontroller;

import com.deyuanyun.pic.common.util.ajax.AjaxSupport;
import com.deyuanyun.pic.common.util.web.Asserts;
import com.deyuanyun.pic.domain.pipe.PipeLine;
import com.deyuanyun.pic.domain.pipe.PipeSegment;
import com.deyuanyun.pic.domain.pipe.ValveRoom;
import com.deyuanyun.pic.pipe.controller.PipeBaseController;
import com.deyuanyun.pic.pipe.support.utils.PipeDictUtils;
import com.deyuanyun.pic.support.biz.CodeUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * Created by xu on 2016/1/14.
 */
@Controller
@RequestMapping("public/dict/")
public class DictController extends PipeBaseController{
    static Logger logger=Logger.getLogger(DictController.class);

    /**
     * 获取场站阀室信息
     * foceFresh 是否强制刷新 yes no
     *
     */
    @RequestMapping("getRooms.json")
    @ResponseBody
    public void getRooms(String foceFresh){
        if (StringUtils.isBlank(foceFresh)||"no".equals(foceFresh)){
            List<ValveRoom> roomList=PipeDictUtils.getRooms(false);
            AjaxSupport.sendSuccessText("roomList",roomList);
            return;
        }
        if ("yes".equals(foceFresh)){
            AjaxSupport.sendSuccessText("roomList",PipeDictUtils.getRooms(true));
            return;
        }

    }


    /**
     * 获取管道列表
     * @param id
     */
    @RequestMapping("getPipeLineList.json")
    @ResponseBody
    public void  getPipeLineList(String id){
        if (StringUtils.isBlank(id)){
            Map<String,String> lineMap= PipeDictUtils.getAllLine();
            AjaxSupport.sendSuccessText("",lineMap);
            return;
        }else {
            PipeLine pipeLine = PipeDictUtils.getLineById(id);
            AjaxSupport.sendSuccessText("",pipeLine);
        }

    }

    /**
     * 根据管道id获取管段
     * @param id
     */
    @RequestMapping("getPipeSegmentByLineId.json")
    @ResponseBody
    public void getPipeSegmentByLineId(String id){
        Asserts.assertTrue(StringUtils.isNotBlank(id),"管道id不能为空");
        List<PipeSegment> segments = PipeDictUtils.getSegmentByLineId(id);
        AjaxSupport.sendSuccessText("",segments);
    }
    /**
     * 等到字典信息
     *
     * @return
     */
    @RequestMapping("codes.json")
    @ResponseBody
    public void getCodes(String majorCode) {
    	
    	 AjaxSupport.sendSuccessText("",CodeUtil.getCodeList(majorCode));
//    	 
//        ModelAndView mv = new ModelAndView();
//        mv.addObject("result", CodeUtil.getCodeList(majorCode));
//        return mv;
    }
}
